<template>
<div class="create card">
    <Form :model="formItem" :label-width="80" ref="form" 
    :rules="ruleInline" >
        <Row>
            <Col span="12">
                <FormItem label="登录名"  prop="name">
                    <Input v-model="formItem.name" placeholder="登录名" disabled></Input>
                </FormItem>
            </Col>
            <Col span="12">
                <FormItem label="昵称"  prop="nickname">
                    <Input v-model="formItem.nickname" placeholder="昵称" ></Input>
                </FormItem>
            </Col>
        </Row>
        <Row>
            <Col span="24">
                <FormItem label="描述" prop="dec">
                    <Input v-model="formItem.dec" type="textarea"  placeholder="描述"></Input>
                </FormItem>
            </Col>
        </Row>
        <Row>
            <Col span="24">
                <FormItem label="头像" >
                    <Upload action="/api/new/upload"  name="picture" 
                    :on-success="handleSuccess" :show-upload-list="false">
                        <Button type="ghost" icon="ios-cloud-upload-outline">Upload files</Button>
                    </Upload>
                </FormItem>
            </Col>
        </Row>
        <div style="margin-left:80px;">
            <Button type="primary" @click="submitform">提交</Button>
        </div>
    </Form>
</div>
</template>
<script>
import axios from "../tools/axios";
export default {
    middleware: 'auth',
    head(){
        return {
            title:'用户编辑'
        }
    },
    async asyncData({ store, params,error,req }){
        //console.log(req.headers)
        //var t= await axios.post('/login/user',{},{headers:req.headers});
        //console.log(t);
        var t= await axios.post('/login/list/'+store.state.user._id);
        
        return {
            formItem:t.data,
            
            ruleInline:{
                name:[
                    {required:true,message:'标题不能为空',trigger: 'blur' }
                ],nickname:[
                    {required:true,message:'昵称不能为空',trigger: 'blur' }
                ],dec:[
                    {required:true,message:'描述不能为空',trigger: 'blur' }
                ]
            }
        }
    },methods:{
        handleSuccess(res){
            console.log(res);
            this.formItem.imgurl=res.data[0];
        },
        settime(item){
            var t=new Date();
            this.formItem.time=item+' '+t.getHours()+':'+t.getMinutes()+':'+t.getSeconds();
        },async submitform(){
            if(this.validate){
                var t= await axios.post('/login/update',this.formItem);
                if(t.errorCode==0){
                    this.$Message.info('修改成功');
                    this.$router.push('/')
                }
            }
        },onEditorChange({ quill, html, text }){
            this.formItem.text = html
        },validate(){
            var a=true;
            this.$refs.form.validate((valid) => {
                if (valid) {
                    //this.$Message.success('Success!');
                    
                } else {
                   a=false;
                }
            })
            return a;
        }
    }
}
</script>